package grp.test.service.Impl;


import grp.test.common.ServerResponse;
import grp.test.dao.BookMapper;
import grp.test.pojo.Book;
import grp.test.service.IInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("iInformationService")
public class InformationImpl implements IInformationService {

    @Autowired(required=false)
    private BookMapper bookMapper;

    @Override
    public ServerResponse<Book> getBookInformation(Integer bookId) {

        Book book = bookMapper.selectByPrimaryKey(bookId);
        if(book == null){
            return ServerResponse.createByErrorMessage("找不到当前书籍！");
        }
        return ServerResponse.createBySuccess(book);
    }


    @Override
    public ServerResponse<Book> updateBookInformation(Book book) {

        Book resultBook = bookMapper.selectByPrimaryKey(book.getBookId());
        if(resultBook == null){
            return ServerResponse.createByErrorMessage("修改书籍不存在！");
        }

        Book updateBook=new Book();
        updateBook.setBookId(book.getBookId());
        updateBook.setBookName(book.getBookName());
        updateBook.setBookType(book.getBookType());
        updateBook.setDes(book.getDes());
        updateBook.setPrice(book.getPrice());
        updateBook.setStore(book.getStore());

        int resultCount = bookMapper.updateByPrimaryKeySelective(book);
        if(resultCount>0){
            return ServerResponse.createBySuccess("更新书籍信息成功！",updateBook);
        }
        return ServerResponse.createByErrorMessage("更新书籍信息失败！");



    }
}
